/**
 * UNIVERSIDADE FEDERAL DO RIO GRANDE DO NORTE - UFRN
 * DEPARTAMENTO DE INFORMATICA E MATEMATICA APLICADA - DIMAP
 * Programa de Pos-Graduacao em Sistemas e Computacao - PPGSC
 */
package br.ufrn.spl.ev.gui.managers;

import java.io.File;

import br.ufrn.spl.ev.PluginConstants;
import br.ufrn.spl.ev.engines.conflicts.textual.TextualConflictsDetection;
import br.ufrn.spl.ev.gui.swt.ShowEvolutionsUI;
import br.ufrn.spl.ev.models.changeloghistorymodel.ChangeLogHistory;
import br.ufrn.spl.ev.models.conflictmodel.ConflictModel;
import br.ufrn.spl.ev.util.ChangeLogHistoryUtil;
import br.ufrn.spl.ev.util.GUIUtils;
import br.ufrn.spl.ev.util.ProductLineConflictModelUtil;

/**
 * <p>
 * Executes the process of analyze textual conflicts
 * </p>
 * 
 * <p>
 * Controls the flow of the tool, call the strategy to make the job, and after
 * call a UI to show the results.
 * </p>
 * 
 * @author Gleydson Lima
 * @vesion 1.0 - Class Creation.
 * @since 16/11/2013
 * 
 */
public class AnalyzeTextualConflictsManager {

	/* Reference to the main UI of the tool */
	private ShowEvolutionsUI evolutionUI;

	public AnalyzeTextualConflictsManager(ShowEvolutionsUI evolutionUI) {
		this.evolutionUI = evolutionUI;
	}

	/**
	 * Make the process the execution of indirect conflicts
	 * 
	 * @throws Exception
	 */
	public void executeTextualConflictsAnalisis(ChangeLogHistory changeLogHistorySource,
			ChangeLogHistory changeLogHistoryTarget, ConflictModel productLineConflictModel)
			throws Exception {

		if (changeLogHistorySource == null) {

			GUIUtils.addErrorMessage("Please, run direct Conflict analisis first!");

		} else {

			new TextualConflictsDetection().checkTextualConflicts(changeLogHistorySource, changeLogHistoryTarget);

			saveAndShowEvolutionResults(changeLogHistorySource, changeLogHistoryTarget);

			System.out.println("/////  TEXTUAL CONFLICT ANALYSIS DONE ");

		}
	}

	/**
	 * <p>
	 * Execute the information for the user and show it for the user in the GUI
	 * </p>
	 * 
	 * <p>
	 * Created at: 14/10/2013
	 * </p>
	 * 
	 * @param startTime
	 * @param changeLogHistorySource
	 * @param changeLogHistoryTarget
	 * @return
	 */
	protected void saveAndShowEvolutionResults(ChangeLogHistory changeLogHistorySource,
			ChangeLogHistory changeLogHistoryTarget) {

		System.out.println("Saving textual conflict information in:  "+evolutionUI.getWorkDirectoryPath());
		
		ChangeLogHistoryUtil.writeHistoryChangeLogFile(changeLogHistorySource, new File(evolutionUI.getWorkDirectoryPath()+PluginConstants.HISTORY_CHANGE_LOG_SOURCE_FILE));
		ChangeLogHistoryUtil.writeHistoryChangeLogFile(changeLogHistoryTarget, new File(evolutionUI.getWorkDirectoryPath()+PluginConstants.HISTORY_CHANGE_LOG_TARGET_FILE));
		ProductLineConflictModelUtil.writeProductLineConflictModelToFile(new File(evolutionUI.getWorkDirectoryPath()+PluginConstants.PRODUCTI_LINE_CONFLICT_MODEL_FILE));
		
		evolutionUI.showEvolutionsAnalisis(changeLogHistorySource, changeLogHistoryTarget, null);

	}
}
